 /*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /**
 * Project  : CdutHelperAndroid
 * Package  : net.solosky.cduthelper.android.adapter
 * File     : SimpleListAdapter.java
 * Author   : solosky < solosky772@qq.com >
 * Created  : 2011-5-20
 * License  : Apache License 2.0 
 */
package net.solosky.easyavatar.view;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 *
 *
 * @author solosky <solosky772@qq.com>
 *
 */
public abstract class SimpleListAdapter<T> extends BaseAdapter{

	private LayoutInflater mInflater;
	private List<T> dataList;
	private Context context;
	
	/**
	 * @param dataList
	 */
	public SimpleListAdapter(Context context, List<T> dataList) {
		this.dataList = dataList;
		this.mInflater = LayoutInflater.from(context);
		this.context = context;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		return dataList.size();
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public Object getItem(int i) {
		return dataList.get(i);
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int i) {
		return 0;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = newView(position);
			holder.contentView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		fillView(holder, position);
		return holder.contentView;
	}
	
	protected Context getContext(){
		return context;
	}
	
	//创建一个新的View
	public abstract ViewHolder newView(int position);
	
	//填充这个View
	public abstract void fillView(ViewHolder viewHolder, int position);
	
	//展开一个View
	public View inflate(int resourceId){
		return this.mInflater.inflate(resourceId, null);
	}
	
	//保存引用，提高效率
	public abstract class ViewHolder{
		public View contentView;
	}

	protected List<T> getDataList() {
		return dataList;
	}
}
